package com.example.heimashop.view.adapter;

import android.view.LayoutInflater;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.heimashop.databinding.ItemCartBinding;
import com.example.heimashop.model.CartItem;
import com.example.heimashop.utils.GlideUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
 * 购物车适配器
 */
public class CartAdapter extends RecyclerView.Adapter<CartAdapter.ViewHolder> {

    private List<CartItem> cartItems = new ArrayList<>();
    private OnCartItemListener listener;

    public interface OnCartItemListener {
        void onCheckedChanged(CartItem item, boolean isChecked);
        void onCountChanged(CartItem item, int count);
        void onDelete(CartItem item);
    }

    public void setListener(OnCartItemListener listener) {
        this.listener = listener;
    }

    public void setData(List<CartItem> list) {
        if (list != null) {
            this.cartItems = list;
            notifyDataSetChanged();
        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemCartBinding binding = ItemCartBinding.inflate(
                LayoutInflater.from(parent.getContext()),
                parent,
                false
        );
        return new ViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        CartItem item = cartItems.get(position);
        holder.bind(item, listener);
    }

    @Override
    public int getItemCount() {
        return cartItems.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private final ItemCartBinding binding;

        ViewHolder(ItemCartBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }

        void bind(CartItem item, OnCartItemListener listener) {
            // 商品信息
            binding.tvName.setText(item.getGoodsName());
            binding.tvPrice.setText(String.format(Locale.CHINA, "¥%.2f", item.getGoodsPrice()));
            binding.tvCount.setText(String.valueOf(item.getGoodsCount()));
            binding.cbSelect.setChecked(item.isChecked());

            // 加载图片
            if (item.getGoodsImage() != null && !item.getGoodsImage().isEmpty()) {
                GlideUtils.loadImage(binding.getRoot().getContext(), item.getGoodsImage(), binding.ivGoods);
            }

            // 选中状态
            binding.cbSelect.setOnCheckedChangeListener((buttonView, isChecked) -> {
                if (listener != null) {
                    listener.onCheckedChanged(item, isChecked);
                }
            });

            // 减少数量
            binding.btnDecrease.setOnClickListener(v -> {
                int count = item.getGoodsCount();
                if (count > 1) {
                    count--;
                    binding.tvCount.setText(String.valueOf(count));
                    if (listener != null) {
                        listener.onCountChanged(item, count);
                    }
                }
            });

            // 增加数量
            binding.btnIncrease.setOnClickListener(v -> {
                int count = item.getGoodsCount();
                count++;
                binding.tvCount.setText(String.valueOf(count));
                if (listener != null) {
                    listener.onCountChanged(item, count);
                }
            });

            // 删除
            binding.btnDelete.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onDelete(item);
                }
            });
        }
    }
}
